package com.jelly.web.share.base.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jelly.common.constant.Constant;
import com.jelly.common.constant.MenuConstant;
import com.jelly.common.constant.ShiroConstant;
import com.jelly.common.util.ListUtil;
import com.jelly.common.util.SensitiveWordsUtils;
import com.jelly.common.util.StringUtils;
import com.jelly.common.util.web.RequestContextHolderUtil;
import com.jelly.common.web.page.LayuiTableInfo;
import com.jelly.common.web.page.PageEntity;
import com.jelly.common.web.page.PageParameter;
import com.jelly.config.redis.RedisCacheManager;
import com.jelly.config.shiro.token.TokenUtil;
import com.jelly.web.modules.basics.dictionary.domain.DiyTypeAndDictionary;
import com.jelly.web.modules.basics.dictionary.service.IDiyTypeAndDictionaryService;
import com.jelly.web.modules.basics.sensitiveWord.service.ISensitiveWordService;
import com.jelly.web.modules.basics.websiteDetails.domain.WebsiteDetails;
import com.jelly.web.modules.basics.websiteDetails.service.IWebsiteDetailsService;
import com.jelly.web.modules.staff.user.domain.UserAndRoleAndPermission;
import com.jelly.web.modules.staff.user.service.IUserService;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheManager;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * @创建人 ql
 * @创建时间 2018-08-21 14:55
 * @项目名称 jelly
 * @功能描述: 参数设定
 **/
public class BaseParameterController {

    public static final String SHIRODICTIONY = "shiro_dictiony";

    @Autowired
    private IDiyTypeAndDictionaryService diyTypeAndDictionaryService;

    @Autowired
    private IUserService userService;

    @Autowired
    private ISensitiveWordService sensitiveWordService;

    @Autowired
    private IWebsiteDetailsService websiteDetailsService;

    @Autowired
    private RedisCacheManager redisCacheManager;
    /**
     * 分页参数设置
     */
    protected void getPageHelper(){

        PageEntity pageEntity = PageParameter.getPageRequest();
        String orderby = pageEntity.getOrderBy();
        String rule = pageEntity.getRule();
        String orderByClause = "";

        if(StringUtils.isNotBlank(orderby)&&StringUtils.isNotBlank(rule)
        ){
            orderByClause = orderby+" "+rule;
        }

        if(pageEntity.getPage() > 0 && pageEntity.getLimit() > 0){

            PageHelper.startPage(pageEntity.getPage(), pageEntity.getLimit(),orderByClause);

        }

    }

    /**
     * 响应请求分页数据
     */
    protected LayuiTableInfo getPageHelper(List<?> list){
        LayuiTableInfo tablePage = new LayuiTableInfo();
        tablePage.setCode(0);
        tablePage.setData(list);
        tablePage.setCount(new PageInfo(list).getTotal());
        return tablePage;
    }

    /**
     * 字典详情获取
     */
    protected void getDictionary(){
        List<DiyTypeAndDictionary> list = new ArrayList<>();

        Cache<String, Object> cache = redisCacheManager.getCache(ShiroConstant.SHIROREDISCACHE);
        Object obj = cache.get(SHIRODICTIONY);

        if(null != obj){
            list = (List<DiyTypeAndDictionary>)obj;
        }else{
            list = diyTypeAndDictionaryService.getDiyTypeAndDictionaryIsState(Constant.SUCCESS);
            cache.put(SHIRODICTIONY,list);
        }

        Map<String,Object> map = new HashMap<>();
        if (ListUtil.isNotList(list)){

            for (DiyTypeAndDictionary diyTypeAndDictionary : list){
                map.put(diyTypeAndDictionary.getDiyTypeRoute(),diyTypeAndDictionary.getDictionaryList());
            }
            RequestContextHolderUtil.getRequest().setAttribute("diy",map);
        }

    }

    /**
     * 按钮详情获取
     */
    protected void getButton(){
        UserAndRoleAndPermission user = userService.getUserAndRoleAndPermission(TokenUtil.getUserId(), MenuConstant.MENUBUTTON,RequestContextHolderUtil.getHttpServletRequestUrl());
        if(null != user){
            RequestContextHolderUtil.getRequest().setAttribute("button",user.getPermissionList());
        }
    }

    /**
     * 敏感词验证
     * @return
     */
    protected Set<String> verificationSensitiveWord(String str){
        SensitiveWordsUtils badUtil = new SensitiveWordsUtils();
        badUtil.addSensitiveWordToHashMap(sensitiveWordService.getSensitiveWordAll());
        return badUtil.getSensitiveWord(str, 2);
    }

}
